<template>
  <div class="main">
    <!-- 头部区域 -->
    <div class="header">
      <div class="left">
        <span class="iconfont iconjiantou2" @click="$router.back()"></span>
        <span class="iconfont iconnew"></span>
      </div>
      <span
        class="follow"
        :class="{
          hasFollow:postlist.has_follow,
          notFollow:!postlist.has_follow
      }"
        @click="handleFollow"
      >{{postlist.has_follow?'已关注':'关注'}}</span>
    </div>
    <!-- 放文章的区域 -->
    <div v-if="postlist.type == 1">
      <h3 class="title">{{postlist.title}}</h3>
      <span
        class="author"
        v-if="postlist.user.nickname"
      >{{postlist.user.nickname}} {{postlist.create_date.split('T')[0]}}</span>
      <div class="content" v-html="postlist.content"></div>
    </div>
    <!-- 放视频区域的div -->
    <div class="vedio" v-if="postlist.type == 2 &&postlist.cover.length >= 1">
      <div class="title2">{{postlist.title}}</div>
      <div class="coverWrapper">
         <video 
              src="https://video.pearvideo.com/mp4/adshort/20200827/cont-1693932-15350047_adpkg-ad_hd.mp4"
              controls
              loop
              poster="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1599041498526&di=8ecee65e615156ee4864c92d72fa61fd&imgtype=0&src=http%3A%2F%2Fc1.hoopchina.com.cn%2Fuploads%2Fstar%2Fevent%2Fimages%2F150729%2F104d5de8baf2c5317f7b9430e7bf276e9ad80bae.jpg"
            ></video>
        <!-- <img :src="postlist.cover[0].url" alt /> -->
        <div class="shipinspan" v-if='video'>
          <span class="iconfont iconshipin"></span>
        </div>
      </div>
      <div class="info">
        {{postlist.user.nickname}}
        {{postlist.comment_length}} 跟帖
      </div>
    </div>
    <div class="actions">
      <div class="actions-item">
        <span
          class="iconfont icondianzan"
          :class="{hasLike: postlist.has_like}"
          @click="handleLike"
        ></span>
        <i>{{postlist.like_length}}</i>
      </div>
      <div class="actions-item">
        <span class="iconfont iconweixin"></span>
        <i>微信</i>
      </div>
    </div>
    <!-- 跟帖部分开始 -->
    <div class="Maincontent">
      <h2>精彩跟帖</h2>
      <div class="hascontent" v-if="commemtlist.length>0">
        <MainContent v-for="(value,index) in commemtlist" :key="index" :data="value"></MainContent>
        <div class="moreBtn" @click="$router.push('/morecontent?id='+$route.query.id)">更多跟帖</div>
      </div>
      <div v-else class="nocontent">还没有评论, 快来抢占沙发吧</div>
    </div>
    <!-- 这里需要从parent组件传值进来才可以得到reloadComment -->
    <iptdom @reloadComment="loadContent"></iptdom>
  </div>
</template>

<script>

import MainContent from "../components/Comment/main";// 引入主评论组件
import iptdom from "../components/Comment/iptdom";//引入评论框组件
export default {
  data() {
    return {
      postlist: {},//因为不需要用到遍历，所以这里可以用对象的方式来声明这个变量
      commemtlist: [], //声明评论获取到的值的变量
    };
  },
  components: {
    MainContent, //注册主评论的组件
    iptdom, //注册输入框
  },
  created() {
    // 当页面加载进入到页面的时候，带着文章的id发起axios请求
    console.log(this.$route.query.id);
    this.$axios({
      url: "/post/" + this.$route.query.id,
    }).then((res) => {
      // 得到文章值的data.data赋值给postlist
      this.postlist = res.data.data;
      // console.log(this.postlist);
    });
    // 文章一进入到页面的时候，需要加载评论
    this.loadingContents();
  },
  methods: {
    loadContent() {
      this.loadingContents();
    },
    // 获取到评论得axios的请求
    loadingContents() {
      this.$axios({
        url: "/post_comment/" + this.$route.query.id,
      }).then((res) => {
        const { data } = res.data;
        if (data.length > 3) {
          data.length = 3;
        }
        this.commemtlist = data;
        console.log(132, this.commemtlist);
      });
    },
    handleFollow() {
      console.log("我要取消关注你了");
      console.log(this.postlist.user.id);
      // 假如有关注，那么进行取消关注的操作
      if (this.postlist.has_follow) {
        this.$axios({
          url: "/user_unfollow/" + this.postlist.user.id,
        }).then((res) => {
          console.log(res);
          console.log(res.data.message);
          if (res.data.message == "取消关注成功") {
            this.postlist.has_follow = false;
          }
        });
      } else {
        this.$axios({
          url: "/user_follows/" + this.postlist.user.id,
        }).then((res) => {
          console.log(res);
          if (res.data.message == "关注成功") {
            this.postlist.has_follow = true;
          }
        });
      }
    },
    handleLike() {
      console.log("哥要来取消赞了");
      this.$axios({
        url: "/post_like/" + this.$route.query.id,
      }).then((res) => {
        console.log(res);
        if (res.data.message === "点赞成功") {
          this.postlist.has_like = true;
          this.postlist.like_length += 1;
        } else {
          this.postlist.has_like = false;
          this.postlist.like_length -= 1;
        }
      });
    },
  },
};
</script>

<style lang="less">
* {
  padding: 0;
  margin: 0;
}
.main {
  padding: 20/360 * 100vw;
  border-bottom: 3px solid #cccccc;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20/360 * 100vw;
    .left {
      vertical-align: center;
      .iconjiantou2 {
        font-size: 15/360 * 100vw;
      }
      .iconnew {
        // font-size: 54/360*100vw;
        display: inline-block;
        transform: scale(3);
        margin-left: 20px;
      }
    }
    .follow {
      padding: 3px 10px;
      background-color: #eeeeee;
      font-size: 14px;
      border-radius: 7px;
      &.notFollow {
        background: red;
        border-color: red;
        color: #fff;
      }
    }
  }
  img {
    width: 100%;
  }
  .title {
    font-size: 18px;
    padding: 0;
    box-sizing: border-box;
  }
  .author {
    line-height: 1.8;
    font-size: 14px;
    color: #888;
  }
  .content {
    margin-top: 20/360 * 100vw;
    font-size: 14px;
    line-height: 1.8;
  }
  .actions {
    display: flex;
    justify-content: space-around;
    margin-top: 20/360 * 100vw;
    .actions-item {
      display: flex;
      align-items: center;
      justify-content: center;
      border: 1px solid #999;
      padding: 5px 10px;
      width: 79/360 * 100vw;
      //  height: 27/360*100vw;
      border-radius: 16px;
      font-size: 14px;
      span {
        margin-right: 10px;
      }
      .iconweixin {
        color: #00c800;
      }
    }
  }
  .hasLike {
    color: red;
    border-color: red;
  }
  .vedio {
    padding: 0 20/360 * 100vw;
    // border-bottom: 2px solid #cccccc;
    .coverWrapper {
      margin-top: 20/360 * 100vw;
      position: relative;
      margin-bottom: 20/360 * 100vw;
        video {
      width: 100%;
    }
      .shipinspan {
        position: absolute;
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        border-radius: 50%;
        background-color: rgba(0, 0, 0, 0.5);
        transform: translateX(-27/360 * 100vw) translateY(-27/360 * 100vw);
        left: 50%;
        top: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
        .iconshipin {
          font-size: 46px;
          color: #ffffff;
        }
      }
    }
    .info {
      color: #333;
      font-size: 14px;
    }
  }
  .Maincontent {
    margin-top: 50/360 * 100vw;
    border-top: 2px solid #cccccc;
    h2 {
      text-align: center;
      padding: 24/360 * 100vw;
      font-size: 20/360 * 100vw;
    }
  }
  .moreBtn {
    margin: 80/360 * 100vw auto;
    font-size: 16/360 * 100vw;
    color: black;
    line-height: 26/360 * 100vw;
    padding: 0 14/360 * 100vw;
    border: 1px solid #ccc;
    text-align: center;
    width: 80/360 * 100vw;
    border-radius: 14/360 * 100vw;
  }

  .nocontent {
    font-size: 14px;
    color: #888;
    text-align: center;
  }
}
</style>